<?php

class Dao
{

	static function new_user_id()
	{
		$key = 'users:id_autoinc';
		return Chap0::$ds->redis()->incr($key);
	}

	static function new_post_id()
	{
		$key = 'posts:id_autoinc';
		return Chap0::$ds->redis()->incr($key);
	}

	static function flash($key,$val=null)
	{
		$key = 'flash:' . $key;
		if ( is_null($val) ){
			$val = G::val($_SESSION,$key);
			unset($_SESSION[$key]);
			return $val;
		}
		$_SESSION[$key] = $val;		
	}

}

class Post
{

	static function find($id)
	{
		$post_id = intval($id);
		return $redis->hgetall("post:{$post_id}");
	}

	static function save(array $post)
	{
		$ret = array(
				'status'	=> false,
				'msg'	=> ''
			);

		$redis = Chap0::$ds->redis();

		$post_id = Dao::new_post_id();

		$user_id = $post['user_id'];
		
		$post = array(
				'id'	=> $post_id,
				'user'	=> $post['user_name'],
				'post'	=> $post['post'],
				'time'	=> G_TIMESTAMP
			);

		$ret['status'] = $redis->hmset("post:{$post_id}", $post);
		if ( $ret['status'] )
		{
			$redis->lpush('posts:ids', $post['id']);
			// $redis->hset('posts:usermap', $post['id'], $user_id);
		}

		return $ret;
	}

	static function all($page=1,$pagenum=30)
	{
		$page = intval($page);
		if ( $page < 1 ) $page = 1;

		$pagenum = intval($pagenum);

		if ( $pagenum < 1 ) $pagenum = 1;
		if ( $pagenum > 120 ) $pagenum = 120;

		$offset_start = ($page - 1) * $pagenum;
		$offset_end = $offset_start + $pagenum - 1;

		$redis = Chap0::$ds->redis();

		$total = $redis->llen('posts:ids');

		$posts = array();

		if ( $total > 0 )
		{
			$ids = $redis->lRange('posts:ids', $offset_start, $offset_end);# 0,0 => 1
			if ( !empty($ids) )
			{
				foreach ($ids as $post_id)
				{
					$posts[] = $redis->hgetall("post:{$post_id}");
				}
			}	
		}
		return array(
				'total'	=> $total,
				'rows'	=> $posts,
				'pages'	=> ceil($total / $pagenum),
				'page'	=> $page,
			);
	}

}

class User
{

	static function find(array $cond)
	{
		if ( !empty($cond['id']) )
		{
			$user_id = intval($cond['id']);
			return $redis->hgetall("user:{$user_id}");
		}

		if ( !empty($cond['name']) )
		{
			$user_id = $redis->hget('users:idmap', $cond['name']);
			if ( !$user_id )
			{
				return null;
			}
			return $redis->hgetall("user:{$user_id}");
		}

		return null;
	}

	static function reg($name, $password)
	{
		$ret = array(
				'status'	=> false,
				'msg'	=> ''
			);

		$name = trim($name);
		$password = trim($password);

		if ( empty($password) || empty($name) )
		{
			$ret['msg'] = '用户名和密码不能为空';
			return $ret;
		}

		if ( strlen($password) < 6 )
		{
			$ret['msg'] = '密码不能少于6个字符';
			return $ret;
		}

		$redis = Chap0::$ds->redis();

		$user_id = $redis->hget('users:idmap', $name);
		if ( $user_id )
		{
			$ret['msg'] = '用户名已被注册';
			return $ret;
		}

		$user_id = Dao::new_user_id();
		$user = array(
				'id'	=> $user_id,
				'name'	=> $name,
				'password'	=> self::password($password),
				'time'	=> G_TIMESTAMP
			);

		$ret['status'] = $redis->hmset("user:{$user_id}", $user);
		if ( $ret['status'] )
		{
			$redis->hset('users:idmap', $user['name'], $user['id']);
		}

		return $ret;
	}

	static function logout()
	{
		unset($_SESSION['Chap0:login']);
	}

	static function current_user()
	{
		return empty( $_SESSION['Chap0:login'] ) ? NULL : $_SESSION['Chap0:login'];
	}

	static function is_login()
	{
		return empty($_SESSION['Chap0:login']) ? false : true;
	}

	static function login($name, $password)
	{
		if ( empty($name) || empty($password) ) return false;

		$redis = Chap0::$ds->redis();
		$user_id = $redis->hget('users:idmap', $name);

		if ( !$user_id ) return false;

		$user = $redis->hgetall("user:{$user_id}");
		if ( !empty($user) )
		{
			if ($user['password'] == self::password($password) )
			{
				$_SESSION['Chap0:login'] = $user;
				return $user;
			}
		}

		return false;	
	}

	static function password($password)
	{
		return md5(__METHOD__ . $password);
	}

}
